//
//  BoundGuideViewController.swift
//  EasePods
//
//  Created by 李然 on 2024/5/20.
//

import UIKit

enum BoundGuideType: String {
    
    case powerOn
    case startEarPair
    case connectEarBT
    case setting
    case welcome
    
    var image: UIImage? {
        switch self {
        case .powerOn: return nil
        case .startEarPair: return R.image.img_bound_open_earcase()
        case .connectEarBT: return R.image.img_bound_bluetooth()
        case .setting: return R.image.img_bound_link()
        case .welcome: return nil
        }
    }
    
    var title: String {
        switch self {
        case .powerOn: return R.string.localizable.guide_ble_power_on_title()
        case .startEarPair: return R.string.localizable.guide_start_earbuds_pair_title()
        case .connectEarBT: return R.string.localizable.guide_connect_earbt_title()
        case .setting: return R.string.localizable.guide_setting_title(Bundle.main.displayName ?? "")
        case .welcome: return ""
        }
    }
    
    var tip: String {
        switch self {
        case .powerOn: return R.string.localizable.guide_ble_power_on_tip()
        case .startEarPair: return R.string.localizable.guide_start_earbuds_pair_tip()
        case .connectEarBT: return R.string.localizable.guide_connect_earbt_tip()
        case .setting: return R.string.localizable.guide_setting_tip()
        case .welcome: return ""
        }
    }
    
    var userDetaultKey: String {
        return "k\(self.rawValue)HasShowedKey"
    }
}

class BoundGuideViewController: ViewController {
    
    var isPresent = false
    var guideType: BoundGuideType = .powerOn
    var nextAction: (() -> ())?
    
    override func makeUI() {
        super.makeUI()
        
        self.navigation.bar.isHidden = true
        
        let boxView = View()
        view.addSubview(boxView)
        boxView.snp.makeConstraints { make in
            make.top.equalTo(kStatusBarHeight + 30)
            make.width.centerX.equalToSuperview()
            make.height.equalTo(300)
        }
        
        boxView.addSubview(topStackView)
        topStackView.snp.makeConstraints { make in
            make.center.equalToSuperview()
        }
        
        view.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(topStackView.snp.bottom).offset(40)
            make.left.equalTo(20)
            make.width.equalTo(kScreenWidth - 40)
        }
        
        view.addSubview(tipLabel)
        tipLabel.snp.makeConstraints { make in
            make.top.equalTo(titleLabel.snp.bottom).offset(8)
            make.left.equalTo(20)
            make.width.equalTo(kScreenWidth - 40)
        }
        
        view.addSubview(sureButton)
        sureButton.addTarget(self, action: #selector(nextClick), for: .touchUpInside)
        sureButton.snp.makeConstraints { make in
            make.bottom.equalTo(-kSafeBottomMargin - 75)
            make.centerX.equalToSuperview()
            make.size.equalTo(CGSize(width: kScreenWidth - 32, height: 57))
        }
        
        titleLabel.text = self.guideType.title
        tipLabel.text = self.guideType.tip
        
        switch self.guideType {
        case .powerOn:
            topStackView.spacing = 20
            let imageView1 = UIImageView(image: R.image.img_bound_power_1())
            topStackView.addArrangedSubview(imageView1)
            
            let label1 = Label()
            label1.text = R.string.localizable.common_or()
            label1.textColor = R.color.common_3_text()
            label1.font = Fonts.systemFont(ofSize: 16)
            topStackView.addArrangedSubview(label1)
            
            let imageView2 = UIImageView(image: R.image.img_bound_power_2())
            topStackView.addArrangedSubview(imageView2)
        case .startEarPair:
            let imageView1 = UIImageView(image: self.guideType.image)
            topStackView.addArrangedSubview(imageView1)
            self.sureButton.setTitle(R.string.localizable.common_next_step(), for: .normal)
        case .connectEarBT:
            let imageView1 = UIImageView(image: self.guideType.image)
            topStackView.addArrangedSubview(imageView1)
            if self.isPresent {
                self.sureButton.setTitle(R.string.localizable.common_ok(), for: .normal)
            } else {
                self.sureButton.setTitle(R.string.localizable.guide_to_system_setting(), for: .normal)
            }
        case .setting:
            let imageView1 = UIImageView(image: self.guideType.image)
            topStackView.addArrangedSubview(imageView1)
        case .welcome:
            break
        }
    }
    
    @objc func nextClick() {
        if self.guideType == .connectEarBT {
            if !self.isPresent {
                if let url = URL(string: UIApplication.openSettingsURLString) {
                    if UIApplication.shared.canOpenURL(url) {
                        UIApplication.shared.open(url, options: [:], completionHandler: nil)
                    }
                }
            }
        }
        if self.guideType == .startEarPair {
            let vc = BoundGuideViewController()
            vc.guideType = .connectEarBT
            vc.nextAction = self.nextAction
            vc.isPresent = self.isPresent
            self.navigationController?.pushViewController(vc, animated: true)
            self.navigationController?.viewControllers.removeAll(where: { vc in
                if let vc = vc as? BoundGuideViewController, vc.guideType == .startEarPair {
                    return true
                }
                return false
            })
            return
        }
        if isPresent {
            self.dismiss(animated: true) {
                self.nextAction?()
            }
        } else {
            self.nextAction?()
            self.navigationController?.viewControllers.removeAll(where: { $0 == self })
        }
    }
    
    lazy var topStackView: UIStackView = {
        let view = UIStackView()
        view.contentMode = .center
        view.axis = .vertical
        view.alignment = .center
        return view
    }()
    
    lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.textColor = R.color.common_3_text()
        label.font = Fonts.systemFont(ofSize: 22)
        label.numberOfLines = 0
        return label
    }()
    
    lazy var tipLabel: UILabel = {
        let label = UILabel()
        label.textColor = R.color.common_23_text_second()
        label.font = Fonts.systemFont(ofSize: 14)
        label.numberOfLines = 0
        return label
    }()

    lazy var sureButton: UIButton = {
        let btn = UIButton(type: .custom)
        btn.layer.cornerRadius = 6
        btn.backgroundColor = R.color.common_22_button_n()
        btn.setTitle(R.string.localizable.common_ok(), for: .normal)
        btn.titleLabel?.font = Fonts.systemFont(ofSize: 18,weight: .semibold)
        btn.setTitleColor(R.color.common_24_button_text(), for: .normal)
        return btn
    }()
}
